package com.dingpeikeji.quickdeer.modules.rcpt.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.common.service.ServiceException;
import com.dingpeikeji.quickdeer.core.modules.orders.dao.TOrdersDao;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.modules.rcpt.dao.TRcptDao;
import com.dingpeikeji.quickdeer.modules.rcpt.dao.TUsersRcptDao;
import com.dingpeikeji.quickdeer.modules.rcpt.entity.TRcpt;
import com.dingpeikeji.quickdeer.modules.rcpt.entity.TUsersRcpt;
import com.google.common.collect.Lists;

/**
 * 用户最新发票信息Service
 * 
 * @author dingpei
 * @version 2017-03-03
 */
@Service
@Transactional(readOnly = true)
public class TUsersRcptService extends CrudService<TUsersRcptDao, TUsersRcpt> {
	@Autowired
	private TOrdersDao ordersDao;
	@Autowired
	private TRcptDao rcptDao;

	public TUsersRcpt get(String alwaysid) {
		return super.get(alwaysid);
	}

	public TUsersRcpt getByUserid(String userid) {
		TUsersRcpt tUsersRcpt = new TUsersRcpt();
		tUsersRcpt.setUserid(userid);
		List<TUsersRcpt> list = super.findList(tUsersRcpt);
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return new TUsersRcpt();
	}

	public List<TUsersRcpt> findList(TUsersRcpt tUsersRcpt) {
		return super.findList(tUsersRcpt);
	}

	public Page<TUsersRcpt> findPage(Page<TUsersRcpt> page, TUsersRcpt tUsersRcpt) {
		return super.findPage(page, tUsersRcpt);
	}

	@Transactional(readOnly = false)
	public void save(TUsersRcpt tUsersRcpt) {
		if (tUsersRcpt.getAlwaysid() == null) {
			dao.insert(tUsersRcpt);
		} else {
			dao.update(tUsersRcpt);
		}
	}

	@Transactional(readOnly = false)
	public void delete(TUsersRcpt tUsersRcpt) {
		super.delete(tUsersRcpt);
	}

	@Transactional(readOnly = false)
	public void genRcpts(String rcptIds, TUsersRcpt ur, User user) {
		try {
			String ids[] = rcptIds.split(",");
			Double money = 0D;
			List<TOrders> orderList = Lists.newArrayList();
			for (String id : ids) {
				TOrders orders = this.ordersDao.get(id);
				orderList.add(orders);
				money += Double.parseDouble(orders.getDueamount());
			}
			if (!orderList.isEmpty()) {
				TRcpt rcpt = new TRcpt();
				rcpt.setTitle(ur.getTitle());
				rcpt.setRcpttype(ur.getRcpttype());
				rcpt.setAddress(ur.getAddress());
				rcpt.setMobile(ur.getMobile());
				rcpt.setReceiver(ur.getReceiver());
				rcpt.setRcptamount(money);
				rcpt.setTotalOutkmsfee(ur.getTotalOutkmsfee());
				rcpt.setTotalOuttimefee(ur.getTotalOuttimefee());
				rcpt.setTotalRirverfee(ur.getTotalRirverfee());
				rcpt.setRcptStatus(TRcpt.RCPT_ST_APPLY);
				rcpt.setCreateDate(new Date());
				rcpt.setCreateBy(user);
				rcpt.setPostcode(ur.getPostcode());
				rcpt.setContent(ur.getContent());
				rcpt.setDutyParagraph(ur.getDutyParagraph());
				rcptDao.insert(rcpt);
				for (TOrders orders : orderList) {
					if (orders.getRcpt() == null || orders.getRcpt().getRcptid() == null) {
						orders.setRcpt(rcpt);
						ordersDao.update(orders);
					}
				}
			}
		} catch (Exception e) {
			throw new ServiceException();
		}
	}

}